package com.wanmait.recycle.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.service.AdminService;

@Controller
@RequestMapping("/manager")
public class AdminController {
	
	@Resource
	private AdminService adminService;
	
	@RequestMapping(value="admin/adminAdd")
	public void adminAdd(){
		
	}
	
	
	
	@RequestMapping(value="admin/adminAdd2",method=RequestMethod.POST)
	public ModelAndView adminAdd2(Admin admin){
		
		ModelAndView mv = new ModelAndView();
		boolean flag = adminService.isAdminByExample(admin);
		if(flag){
			String msg="exist";
			mv.addObject("msg", msg);
			mv.setViewName("/manager/admin/adminAdd");
		}
		else {
			
			adminService.insertAdmin(admin);
			mv.setViewName("redirect:/manager/admin/adminSuperList");
		}
		return mv;
	}
	
	@RequestMapping("admin/deleteAdmin")
	@ResponseBody
	public void deleteAdmin(Integer id) {
		adminService.deleteAdmin(id);
		
	}
	
	@RequestMapping("admin/deleteAdmins")
	@ResponseBody
	public void deleteAdmins(String ids) {
		adminService.deleteAdmins(ids);
		
	}
	
	@RequestMapping("admin/reAddAdmin")
	@ResponseBody
	public void reAddAdmin(Integer id) {
		adminService.reAddAdmin(id);
		
	}
	
	@RequestMapping("admin/adminSuper")
	public void adminSuper(){
		
	}
	
	@RequestMapping("admin/adminSuperList")
	public ModelAndView adminSuperList(){
		ModelAndView mv = new ModelAndView();
		PageInfo<Admin> admins = adminService.findAllByPage();
		Integer superCount = adminService.selectSuperCount();
		mv.addObject("superCount", superCount);
		mv.addObject("admins", admins);
		return mv;
	}
	
	@RequestMapping("admin/adminList")
	public ModelAndView adminList(){
		ModelAndView mv = new ModelAndView();
		List<Admin> admins = adminService.findAll();
		mv.addObject("admins", admins);
		return mv;
	}
	
	@RequestMapping("admin/changePassword")
	public void changePassword(Integer id,String pass){
		
	}
	
	@RequestMapping("admin/upAdmin")
	@ResponseBody
	public void upAdmin(Integer id){
		adminService.up(id);
	}
	
	@RequestMapping("admin/downAdmin")
	@ResponseBody
	public void downAdmin(Integer id){
		adminService.down(id);
	}
	
	@RequestMapping("admin/checkPassword")
	
	public String checkPassword(Integer id,String newpassword2){
		
		adminService.updateById(id,newpassword2);
		
		return "redirect:/manager/managerIndex";
	}
}
